package com.ymttest.database.sqlwapper.anlihui;

import java.util.HashMap;
import java.util.List;

import org.apache.ibatis.exceptions.PersistenceException;

import com.ymt.database.SqlManager;
import com.ymttest.database.model.anlihui.InviteRelation;

public class InviteRelationWrapper {

	SqlManager manager;

    public InviteRelationWrapper() {
        manager = new SqlManager("anlihui");
    }
    
    @SuppressWarnings("unchecked")
	public InviteRelation selectByPrimaryKey(Long id) {
    	
    	InviteRelation inviteRelation = new InviteRelation();
    	List<InviteRelation> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.anlihui.InviteRelationMapper.selectByPrimaryKey",
                        id);
        manager.commit();
        
		if (result.size() > 0) {
			inviteRelation = result.get(0);
		}
		
        return inviteRelation;
    }
    
    @SuppressWarnings("unchecked")
	public InviteRelation selectByInviteeIdAndInviterId(Long inviteeId, Long inviterId) {
    	
    	HashMap<String, Long> map = new HashMap<>();
    	
    	map.put("invitee_id", inviteeId);
    	map.put("inviter_id", inviterId);
    	
    	InviteRelation inviteRelation = new InviteRelation();
    	List<InviteRelation> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.anlihui.InviteRelationMapper.selectByInviteeIdAndInviterId",
                        map);
        manager.commit();
        
		if (result.size() > 0) {
			inviteRelation = result.get(0);
		}
		
        return inviteRelation;
    }
        
	public int insert(InviteRelation inviteRelation) {

		int count = 0;

		try {
			count = manager
					.insert("com.ymttest.database.mapping.anlihui.InviteRelationMapper.insert",
							inviteRelation);
			manager.commit();

		} catch (PersistenceException e) {
			// 如果插入了重复的记录，则删除原来的记录再插入一次
			e.printStackTrace();
		}

		return count;
	}
	
	public int updateBuyProductTime(Long inviteeId, Long inviterId) {

		HashMap<String, Object> map = new HashMap<>();
		
		map.put("invitee_id", inviteeId);
		map.put("inviter_id", inviterId);

		int count = manager
				.update("com.ymttest.database.mapping.anlihui.InviteRelationMapper.updateBuyProductTime",
						map);
		manager.commit();

		return count;
	}
	
	public int updateBuyVipTime(Long inviteeId, Long inviterId) {

		HashMap<String, Object> map = new HashMap<>();
		
		map.put("invitee_id", inviteeId);
		map.put("inviter_id", inviterId);

		int count = manager
				.update("com.ymttest.database.mapping.anlihui.InviteRelationMapper.updateBuyVipTime",
						map);
		manager.commit();

		return count;
	}
    
    public static void main(String[] args) {
    	
    	InviteRelationWrapper test = new InviteRelationWrapper();
    	System.out.println(test.selectByInviteeIdAndInviterId(2222L, 1111L).getInvite_type());
    }
}
